package com.ssm.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.model.LoginInfo;
import com.ssm.model.ProfessorModel;
import com.ssm.model.TeacherModel;
import com.ssm.service.LoginService;
import com.ssm.service.ProfessorService;
import com.ssm.service.TeacherService;
import com.ssm.util.SendEmail;

@Controller
public class LoginController {

	@Autowired
	private LoginService loginService;
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private ProfessorService professorService;

	/**
	 * 登录页
	 * @return  返回登录页
	 */
	@RequestMapping(value = "/login.do",method = RequestMethod.GET)
	public String showLogin(){
		return "login";
	}

	/**
	 * 未授权页面
	 * @return 登录失败，返回未认证页面
	 */
	@RequestMapping("/unauthorized.do")
	public String unauthorized(){
		return "unauthorizedUrl";
	}

	/**
	 * 登录操作
	 * @param loginInfo 登录用户信息
	 * @return 登陆成功-查询角色-重定向到相应首页显示；登录失败-返回登录页面
	 */
	@RequestMapping(value = "/login.do",method = RequestMethod.POST)
	public String doLogin(LoginInfo loginInfo){
		String username = loginInfo.getLoginId().toString();
		String password = loginInfo.getPassword();
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		Subject subject = SecurityUtils.getSubject();
		subject.login(token);
		if(subject.hasRole("admin")) {
			return "redirect:admin/index.do";
		}else if (subject.hasRole("teacher")) {
			return "redirect:teacher/index.do";
		}else if (subject.hasRole("professor")) {
			return "redirect:professor/index.do";
		}
		return "login";
	}

	//重置密码1
	/**
	 *  忘记密码
	 * @return 返回未登录重置密码页面
	 */
	@RequestMapping(value = "/forgetpwd.do",method = RequestMethod.GET)
	public String getPassword(){
		return "teacher/forgetPwd";
	}

	//重置密码
	/**
	 *  忘记密码-1-认证登录id是否有效
	 * @param loginId 需要重置密码的账户id
	 * @return 返回指定账户的email，用于二次验证
	 */
	@ResponseBody
	@RequestMapping("/checkAccount.do")
	public Object resetPassword1(String loginId){
		TeacherModel teacherModel = teacherService.selectTeacherById(Integer.valueOf(loginId));
		String email = teacherModel.gettEmail();
		if(teacherModel==null){
			ProfessorModel professorModel = professorService.selectById(Integer.valueOf(loginId));
			email = professorModel.getpEmail();
		}
		HashMap map = new HashMap<>();
		map.put("email", email);
		return map; 
	}
	//重置密码2
	/**
	 *  忘记密码-2-邮箱验证成功，发送验证码
	 * @param email 接收验证码邮箱
	 * @param loginId 重置密码的用户id
	 * @param request
	 * @return 验证码发送结果json
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/email.do")
	public Object sendEmail(String email,String loginId,HttpServletRequest request) throws Exception{
		Integer verifyCode = SendEmail.sendEamil(email);
		TeacherModel teacherModel = teacherService.selectTeacherById(Integer.valueOf(loginId));
		if(teacherModel==null){
			professorService.updateVerifyByEmail(Integer.valueOf(loginId), email, verifyCode.toString());
		}else{
			//生成验证码的同时向数据库中添加验证码
			teacherService.updateVerifyByEmail(Integer.valueOf(loginId),email, verifyCode.toString());			
		}
		HashMap map = new HashMap<>();
		map.put("success", true);
		return map;
	}
	//重置密码3
	/**
	 *  忘记密码-3-验证验证码
	 * @param email 接收验证码邮箱
	 * @param verify 输入的验证码
	 * @param loginId 充值账户id
	 * @param model
	 * @return 认证成功-跳转到重置密码界面
	 */
	@RequestMapping("/checkVerify.do")
	public String showResetPwd(String email,String verify,String loginId,Model model){
		Integer i = teacherService.checkVerify(Integer.valueOf(loginId), email, verify);
		if(i==0){
			professorService.checkVerify(Integer.valueOf(loginId), email, verify);
		}
		model.addAttribute("loginId",loginId);
		return "teacher/resetPwd";
	}
	//重置密码4
	/**
	 *  重置密码功能
	 * @param password 新密码
	 * @param loginId 充值账户id
	 * @param request
	 * @return 操作结果json
	 */
	@ResponseBody
	@RequestMapping(value = "/resetPwd.do",method = RequestMethod.POST)
	public Object postPassword(String password,String loginId,HttpServletRequest request){
		loginService.resetPwd(password, Integer.valueOf(loginId));
		HashMap map = new HashMap<>();
		map.put("msg", "密码重置成功，请重新登陆！");
		return map;
	}
}
